package scau.erps.view.business.staff;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.Line;
import scau.erps.entity.Team;
import scau.erps.service.LineService;
import scau.erps.service.impl.LineServiceImpl;

/**
 * 设备商界面类
 * @author FYJ
 *
 */
public class EquipmentPanel extends BusinessPanelTemplate{
	private static final long serialVersionUID = 1L;
	private EquipmentTableModel dataModel;
	public EquipmentPanel(){
		super();
		this.setComboItemsValue();		
		this.operationBtn1.setText("变卖"); 
		this.operationBtn2.setText("转产");
		this.operationBtn3.setText("更新");
		this.dataModel = getEquipmentTableModel();
		this.table.setModel(dataModel);
	}
	
	public EquipmentTableModel getEquipmentTableModel() {
		Team team = Erps.getCurrentTeam();
		LineService lineService = Erps.getIocInstance().get(
				LineServiceImpl.class);
		List<Line> list = null;
		if (team == null) { // 工作人员登陆
			list = lineService.getAllLine();
		} else {
			list = lineService.getLine(team.getId(), null, null);
		}
		dataModel = new EquipmentTableModel(list);
		return dataModel;
	}
		
	@Override
	public void setComboItemsValue() {
		String[] euipmentType = new String[]{"生产线类型","手工","半自动","全自动","柔性"};
		String[] equipmentStatus = new String[]{"建设状态","空置","在建","全新","转产","生产","空闲"};
		this.addItems(businessType, euipmentType);
		this.addItems(businessStatus,equipmentStatus);		
	}

	@Override
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == searchBtn){//筛选
			String team = null;
			if (teamCombo.getSelectedIndex() != 0) {
				team = (String) teamCombo.getSelectedItem();
			}			
			String type = null;
			if (businessType.getSelectedIndex() != 0) {
				type = (String) businessType.getSelectedItem();
			}
			String status = null;
			if (businessStatus.getSelectedIndex() != 0) {
				status = (String) businessStatus.getSelectedItem();
			}
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(2);
			if(team !=null){
				filters.add(RowFilter.regexFilter(team));
			}
			if (type != null) {
				filters.add(RowFilter.regexFilter(type));
			}
			if (status !=null){
				filters.add(RowFilter.regexFilter(status));
			}				
			sorter.setRowFilter(RowFilter.andFilter(filters));			
			this.table.setRowSorter(sorter);
			this.table.setModel(dataModel);
		}
		else if(e.getSource()==approveBtn){//审核
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int lineId = (int) this.table.getValueAt(row, 0);
				LineService lineService = Erps.getIocInstance().get(
						LineServiceImpl.class);
				Line line = lineService.getLineById(lineId);
				if ("空置".equals(line.getStatus())) {
					line.setStatus("空闲");
					lineService.updateLine(line);
					String message = "生产线类型:" + line.getType()
							+ "审批通过" ;
					JOptionPane.showConfirmDialog(this, message, "提示",
							JOptionPane.CLOSED_OPTION);
					this.table.setModel(getEquipmentTableModel());
				} else {
					JOptionPane.showConfirmDialog(this, "生产线已审核", "提示",
							JOptionPane.CLOSED_OPTION);
				}
			}
		}
		else if(e.getSource()==operationBtn1){//变卖
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int lineId = (int) this.table.getValueAt(row, 0);
				LineService lineService = Erps.getIocInstance().get(
						LineServiceImpl.class);
				Line line = lineService.getLineById(lineId);
				if ("空闲".equals(line.getStatus())) {
					line.setStatus("空置");
					lineService.updateLine(line);
					String message = "生产线类型:" + line.getType()
							+ "已变卖" ;
					JOptionPane.showConfirmDialog(this, message, "提示",
							JOptionPane.CLOSED_OPTION);
					this.table.setModel(getEquipmentTableModel());
				} else {
					JOptionPane.showConfirmDialog(this, "生产线已变卖", "提示",
							JOptionPane.CLOSED_OPTION);
				}
			}
		}
		else if(e.getSource()==operationBtn2){//转产
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int lineId = (int) this.table.getValueAt(row, 0);
				LineService lineService = Erps.getIocInstance().get(
						LineServiceImpl.class);
				Line line = lineService.getLineById(lineId);
				if ("空闲".equals(line.getStatus())) {
					line.setStatus("转产");
					lineService.updateLine(line);
					String message = "生产线类型:" + line.getType()
							+ "已转产" ;
					JOptionPane.showConfirmDialog(this, message, "提示",
							JOptionPane.CLOSED_OPTION);
					this.table.setModel(getEquipmentTableModel());
				} else {
					JOptionPane.showConfirmDialog(this, "生产线已转产", "提示",
							JOptionPane.CLOSED_OPTION);
				}
			}
		}
		else if(e.getSource()==operationBtn3){
			this.table.setModel(getEquipmentTableModel());
		}
	}

}

class EquipmentTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private String[] title = { "序号", "小组", "生产线号", "产品", "生产线类型", "生产时间",
			"剩余价值", "状态", "转产时间" };
	private Object[][] data = null;

	public EquipmentTableModel(List<Line> list) {
		data = new Object[list.size()][9];
		Iterator<Line> iter = list.iterator();
		for (int i = 0; i < list.size(); i++) {
			Line line = iter.next();
			data[i][0] = line.getId();
			data[i][1] = "第"+line.getTeam_Id()+"组";
			data[i][2] = line.getLineNo();
			data[i][3] = line.getProduct();
			data[i][4] = line.getType();
			data[i][5] = line.getInvestTime();
			data[i][6] = line.getValue();
			data[i][7] = line.getStatus();
			data[i][8] = line.getSwitchTime();
		}
	}

	@Override
	public int getRowCount() {
		return this.data.length; // 行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; // 列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex]; // 取表格单元格值
	}

	public String getColumnName(int columnIndex) {
		return title[columnIndex]; // 显示标题
	}
}